﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Domain.DAL;
using Domain.BLL;

namespace ComputerShop_1611
{
    public partial class Form1 : Form
    {
        //ComputerShopEntities CSE;
        CSEntities CS;
        Binding binding;
        public Form1()
        {
            InitializeComponent();
            //CSE = new ComputerShopEntities();
            CS = new CSEntities();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IEnumerable<ProductBLL> lp = new List<ProductBLL>();
            ProductBLL _p = new ProductBLL();
            lp = _p.listAllProducts();
            dataGridViewProducts.DataSource = lp;

            dataGridViewProducts.Columns["Picture"].Visible = false;

            //textBoxName.DataBindings.Clear();
            textBoxName.DataBindings.Add("Text", lp, "ProductName");
            //textBoxDescription.DataBindings.Clear();
            textBoxDescription.DataBindings.Add("Text", lp, "Description");
            //textBoxID.DataBindings.Clear();
            textBoxID.DataBindings.Add("Text", lp, "ProductID");
            //textBoxPriceIn.DataBindings.Clear();
            binding = textBoxPriceIn.DataBindings.Add("Text", lp, "PriceIn");
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            ProductBLL _p = new ProductBLL();
            _p.ProductID = 7;
            _p.SingleRead();
            textBoxName.Text = _p.ProductName;

             
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            ProductBLL _p = new ProductBLL();
            //textBoxPriceIn.DataBindings.Remove(binding);
            _p.ProductID = Convert.ToInt32(textBoxID.Text);
            _p.ProductName = textBoxName.Text;
            string s = textBoxPriceIn.Text;
            if (textBoxPriceIn.Text.IndexOf('.') != -1)
                s = textBoxPriceIn.Text.Substring(0, textBoxPriceIn.Text.Length - textBoxPriceIn.Text.IndexOf('.') + 1);
            
            _p.Description = textBoxDescription.Text;
            _p.PriceIn = Convert.ToInt64(textBoxPriceIn.Text);
            _p.Update();
            //dataGridViewProducts.Refresh();
            //Form1_Load(null, EventArgs.Empty);
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = new DialogResult();
            Form2 frm2 = new Form2();
            frm2.Location = new Point(100, 700);
            dr = frm2.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //MessageBox.Show("User clicked OK button");

                //
                ProductBLL _p = new ProductBLL();
                _p.ProductID = Convert.ToInt32(textBoxID.Text);

                _p.SingleDelete();

                Form1_Load(null, EventArgs.Empty);
                //
                //else if (dr == DialogResult.Cancel)
                //MessageBox.Show("User clicked Cancel button");
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            Form1_Load(null, EventArgs.Empty);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ReportViewer r = new ReportViewer();
            r.Show();
        }

        

    }
}
